package com.hbu.product.hbumallproduct.controller;

import com.hbu.common.utils.PageUtils;
import com.hbu.common.utils.R;
import com.hbu.product.hbumallproduct.entity.ProductAttrValueEntity;
import com.hbu.product.hbumallproduct.service.AttrService;
import com.hbu.product.hbumallproduct.service.ProductAttrValueService;
import com.hbu.product.hbumallproduct.vo.AttrUpdateVO;
import com.hbu.product.hbumallproduct.vo.AttrVO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author 90458
 */
@RestController
@RequestMapping("hbumallproduct/attr")
@AllArgsConstructor
public class AttrController {

    private AttrService attrService;

    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }

    @RequestMapping("/base/listforspu/{spuId}")
    public R baseAttrList(@PathVariable Long spuId) {
        return R.ok().put("data", productAttrValueService.getBaseAttrList(spuId));
    }

    @RequestMapping(value = "/update/{spuId}",method = RequestMethod.POST)
    public R updateSpuAttr(@PathVariable Long spuId, @RequestBody List<ProductAttrValueEntity> list){
        productAttrValueService.updateSpuAttr(spuId,list);
        return R.ok();
    }


    @RequestMapping(value = "/{attrType}/list/{catelogId}", method = RequestMethod.GET)
    public R baseList(@RequestParam Map<String, Object> params, @PathVariable Long catelogId, @PathVariable String attrType) {
        PageUtils page = attrService.getAttrList(params, catelogId, attrType);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        return R.ok().put("attr", attrService.getInfoById(attrId));
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVO attr) {
        attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrUpdateVO attr) {
        attrService.cascadeUpdate(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

}
